﻿//Security.loadPolicyFile("http://tada.mcmaster.ca/wikita/pub/Main/FlashTAT/crossdomain.xml");

//Packages to Import
import fl.controls.Button;
import fl.controls.DataGrid;
import fl.controls.Label;
import fl.controls.List;
import fl.controls.TextArea;
import fl.controls.UIScrollBar;

import fl.data.DataProvider;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;

import flash.events.*;

import flash.external.ExternalInterface;

import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLVariables;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;

import fl.transitions.*;
import fl.transitions.easing.*;
import fl.events.ListEvent;

import com.porganized.vis.wordcloud;

//import com.porganized.vis.ctree;
//var ctree1:ctree = new ctree();
//addChild(ctree1);

//sets scalemode, so that size stay the same regardless of stage proportions
stage.scaleMode = StageScaleMode.NO_SCALE;
var debug = true;
//add Categories

//Define permanent variables
var i:int;
var sampleItem:Object;
var tool:MovieClip;
var options:MovieClip;
var advOptions:MovieClip;
var Results:MovieClip;
var aboutCat:MovieClip;
//var labelField:TextField = new TextField();
//labelField.multiline=true;
//labelField.wordWrap = true;

var patternField:TextField = new TextField();
var patternSelect:TextArea = new TextArea();
var runTool:Button = new Button();
var toolInfo:TextField = new TextField();
toolInfo.multiline=true;
toolInfo.wordWrap = true;
var totalCats:int = 0;
//Variables of Category headers
var CategoriesArray:Array = new Array();

tool = new CategoryHeader();
addChild(tool);
CategoriesArray["tool"] = {title:"Tool : List Words", object: tool, tool:openTools, position:1};

Results = new CategoryHeader();
addChild(Results);
CategoriesArray["Results"] = {title:"Results", object: Results, tool:openResults, position:2};

aboutCat = new CategoryHeader();
addChild(aboutCat);
CategoriesArray["about"] = {title:"About", object: aboutCat, tool:openAbout, position:3};
//Count number of Categories
for (var catHeadName:String in CategoriesArray) {
	totalCats+=1;
}
//CATEGORY VARIABLES
var toolWidth = stage.stageWidth;
var widthDif = 200-toolWidth;//difference between the rendered stage and the stage set on the webpage
var toolHeight = stage.stageHeight;
var heightDif = 400-toolHeight;
//the y distance between category headers.  Currently, it is shown as their height, 15, plus 1 unit extra
var distanceBetween:Number=16;
var endPoint="http://tapor1-dev.mcmaster.ca/~restserv/html/";
//the position of the top category.  All the others are positioned accordingly
var toolTop:Number=heightDif/2;
var toolEnd:Number=(stage.stageHeight-(distanceBetween*totalCats))+toolTop; 
//since when the height is differerent, zero is not at the expected to, the code needs to account for that by adding the toolTop variables

//STYLE VARIABLESgjhghgfh
//Label Text Style
var labelFormat:TextFormat;
labelFormat = new TextFormat();
labelFormat.font = "Arial, Helvetica";
labelFormat.color = 0xFFFFFF;
labelFormat.size = 11;
var opString;
var allOptions;
var datagridPadding = 10;

//NETWORK VARIABLES
var htmlText:String;
var defaultSortCrit:int = 2;
var defaultListOp:int = 3;
var defaultPattern:String = "text";
var defaultContext:int = 1;//Words
var defaultContextLength:String = "5";
var dg:DataGrid;
var dp:DataProvider;
var pageURL:String = "illiad1.html";
var currentCat:int;
var variables:URLVariables=new URLVariables;
var TAPORresult:XML=new XML;
var cloudField1:TextField = new TextField();
cloudField1.wordWrap = true;
cloudField1.multiline = true;
var concordanceField:TextField = new TextField();
concordanceField.wordWrap = false;
concordanceField.multiline = true;
var concordanceStyle = "text";
var concordanceText = "";
var sb= new UIScrollBar();

//EXTERNAL INTERFACE VARIABLES
if (ExternalInterface.available) {
	var src = ExternalInterface.call('document.getElementsByTagName("embed")[0].src.toString');

	//URL OF THE CURRENT PAGE
	pageURL = ExternalInterface.call('window.location.href.toString');
	if (pageURL==null) pageURL ="illiad1.html";
	
	htmlText = ExternalInterface.call('function returnBodyText(){return escape("<body>"+ document.body.innerHTML+ "</body>");}');
	if (htmlText==null) htmlText ="Error";

	//AND PARAM OPTIONS
	opString = ExternalInterface.call('document.getElementsByName("TATOptions")[0].value.toString');
	if (opString==null) opString="endPoint:url(http://tapor1-dev.mcmaster.ca/~restserv/html/)"; 			
	allOptions = colonSplit (opString);
	
} else {
	//trace("There has been an error with the external interface, or it is not available");
}

//adding default values when they don't exist in the options
if (!allOptions["endPoint"]) allOptions["endPoint"]="http://tapor1-dev.mcmaster.ca/~restserv/html/";
if (!allOptions["htmlTag"]) allOptions["htmlTag"]="body";
if (!allOptions["defaultTool"]) allOptions["defaultTool"]="Word Cloud";
if (!allOptions["header-color"]) allOptions["header-color"]="#FF9900";
if (!allOptions["results-header-color"]) allOptions["results-header-color"]="#0EC5FF";
if (!allOptions["background-color"]) allOptions["background-color"]="#333333";
if (!allOptions["link-color"]) allOptions["link-color"] = allOptions["results-header-color"];
if (!allOptions["stopList"]) allOptions["stopList"] = "glasgow";

setupColors(allOptions["background-color"], allOptions["results-header-color"], allOptions["header-color"]);
include 'CSSstyle.as';
include 'CatSetup.as';
include 'network.as';
HTMLloaded2();
categoryFirstRun();